﻿//Javascript validation for student registration page implemented By EVGENIY (JACK) TSIGANKOV
//You can write other function for validation on the client side and use this "library"

//Validate login form login and password in the Welcome page
function validateLogin() {
    if (document.getElementById("txtUsername").value == "" || document.getElementById("txtUsername").value.length > 21) {
        alert("Invalid username. Please re-enter your username");
        document.getElementById("txtUsername").select();
        return false;
    }
    else if (document.getElementById("txtPassword").value == "" ||
             document.getElementById("txtPassword").value.length > 21 ||
             document.getElementById("txtPassword").value.length < 8) {
            alert("Invalid password. Please re-enter your password");
            document.getElementById("txtPassword").select();
        return false;
    }
    return true;
}

//validate password requirements for customer
function validatePW(p) {
    if (p.length < 8) {
        alert("Password must have at least 8 characters");
        document.getElementById("txtb_password").select();
        return false;
    } else if (p.length > 21) {
        alert("Password must be less than 20 characters long!");
        document.getElementById("txtb_password").select();
        return false;
    }
    var cnt = 0;
    for (var i = 0; i < p.length; i++) {
        if (!isNaN(p[i]) && p[i] != " ")
            cnt++;
    }
    if (cnt == 0) {
        alert("Password must have at least one number!");
        document.getElementById("txtb_password").select();
        return false;
    }
    return true;
}

//insures an email is of the right format
function validateEmail(elementValue) {
    var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    if (emailPattern.test(elementValue)) {
        return true;
    }
    else {
        return false;
    }
}


// validate registration form

function validateForm() {
    var fname = document.getElementById("txtb_first_name").value;
    var lname = document.getElementById("txtb_last_name").value;
    var em = document.getElementById("txtb_email").value;
    var cem = document.getElementById("txtb_email_confirm").value;
    // var pw = document.getElementById("txtb_password").value;
    // var cpw = document.getElementById("txtb_password_confirm").value;
    var stAddress = document.getElementById("txtb_street_address").value;
    var ct = document.getElementById("txtb_city").value;
    var zip = document.getElementById("txtb_zipcode").value;
    // var ddlstate = document.getElementById("ddlStates");

    if (!validateAlphabeticString(fname)) {
        alert("First name can only have a-A to z-Z characters with first letter capitalized");
        document.getElementById(fname).select();
        return false;

    } else if (!validateAlphabeticString(lname)) {
        alert("Last name can only have a-A to z-Z characters with first letter capitalized");
        document.getElementById(lname).select();
        return false;

    } else if (!validateEmail(em)) {
        alert("Invalid email address");
        document.getElementById(em).select();
        return false;
    } else if (em != cem) {
        alert("Email doesn't match");
        document.getElementById(em).select();
        return false;

    }
    /* else if (!validatePW(pw))
    {
    return false;
    } else if (pw != cpw)
    {
    alert("Password doesn't match");
    document.getElementById(pw).select();
    return false;
    } */

    else if (!validateStreet(stAddress)) {
        alert("Street address should start with at least one number, followed by an empty space and have more than 5 characters.");
        document.getElementById(stAddress).select();
        return false;
    } else if (!validateAlphabeticString(ct)) {
        alert("City should be all letters and starts with a capital letter");
        document.getElementById(ct).select();
        return false;
    }

    /*else if (!validateState(ddlstate))
    {
    alert("Select State");
    document.getElementById(ddlstate).focus();
    return false;

    } 
    */

    else if (!validateZipCode(zip)) {
        alert("Zip code must be a 5-digit number");
        document.getElementById(zip).select();
        return false;
    } else
        return true;
}




//insure a string contains only alphabetic characters
function validateAlphabeticString(name) {
    var emailPattern = /^[A-Z][a-zA-Z]+[\sa-zA-Z]*/;
    return emailPattern.test(name);
}




//validate street address for new customer
function validateStreet(st) {
    var patt = /^[1-9]{1,5}\s[a-zA-Z]+[\sa-zA-Z]+/;
    return patt.test(st);
}

function validateState(ddl) {
    // ddl = document.getElementById("ddlStates");
    var selectedState = ddl.options[ddl.selectedIndex].value;
    if (selectedState == "--Select State--") { return false; }
    return true;
}
function validateZipCode(zc) {
    var pattern = /^\d{5}$/;
    return pattern.test(zc);
}



function Exit() {
    // window.opener = null;
    //window.close();
    // window.opener = 'X';
    window.open('', '_parent', '');
    window.close();
}
function clearText(txt) {
    txt.value = '';
    if (txt == document.getElementById("txtb_password")) {
        txt.type = "password";
    }
}